package work.mediway.mdm.controller.biz;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.mediway.hos.app.base.seure.util.SecurityUtils;
import com.mediway.hos.base.model.BaseResponse;
import com.mediway.hos.log.annotation.OperLog;
import com.mediway.hos.messgateway.model.entity.MessageType;
import com.mediway.hos.messgateway.service.MessageTypeService;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

import javax.validation.Valid;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import work.mediway.mdm.core.page.QueryRequest;
import work.mediway.mdm.core.annotation.OpsLog;
import work.mediway.mdm.core.enums.CodeAndMessageEnum;
import work.mediway.mdm.core.enums.OpsLogTypeEnum;
import work.mediway.mdm.core.exception.DataException;
import work.mediway.mdm.biz.entity.biz.SsModelInfo;
import work.mediway.mdm.biz.service.biz.SsModelInfoService;

/**
 * (mdm_User.SS_Model_Info)表控制层
 *
 * @author xxxxx
 */
@Api(tags = "服务消息管理模块")
@ApiSupport(order = 27)
@Slf4j
@Validated
@RestController
@RequestMapping("/model/api")
@RequiredArgsConstructor
public class SsModelInfoController {

    private final SsModelInfoService ssModelInfoService;
    private final MessageTypeService messageTypeService;

    @OpsLog(value = "获取字典消息信息", type = OpsLogTypeEnum.SELECT)
    @ApiOperation(value = "获取字典消息信息")
    @ApiOperationSupport(order = 1)
    @GetMapping
    public BaseResponse<Page<SsModelInfo>> getByModelInfo(SsModelInfo ssModelInfo, QueryRequest queryRequest) {
        Page<SsModelInfo> ssModelInfos = this.ssModelInfoService.getByModelInfo(ssModelInfo, queryRequest);
        return BaseResponse.success(ssModelInfos);
    }

    @OpsLog(value = "保存字典消息信息", type = OpsLogTypeEnum.INSERT)
    @OperLog(title = "保存字典消息信息", content = "保存字典消息信息")
    @ApiOperation(value = "保存字典消息信息")
    @ApiOperationSupport(order = 3)
    @PostMapping
    public BaseResponse<SsModelInfo> saveByModelInfo(@RequestBody @Valid SsModelInfo ssModelInfo) {
        ssModelInfo.setUpdateUser(SecurityUtils.getLoginName());
        ssModelInfo.setUpdateDate(DateUtil.now());
        this.ssModelInfoService.saveOrUpdate(ssModelInfo);
        return BaseResponse.success();
    }

    @OpsLog(value = "删除字典消息信息", type = OpsLogTypeEnum.DELETE)
    @OperLog(title = "删除字典消息信息", content = "删除字典消息信息")
    @ApiOperation(value = "删除字典消息信息")
    @ApiOperationSupport(order = 5)
    @PostMapping("delete")
    public BaseResponse<SsModelInfo> deleteByModelInfo(@RequestBody List<String> ids) {
        this.ssModelInfoService.removeByIds(ids);
        return BaseResponse.success();
    }

    @OpsLog(value = "更新字典消息信息", type = OpsLogTypeEnum.UPDATE)
    @OperLog(title = "更新字典消息信息", content = "更新字典消息信息")
    @ApiOperation(value = "更新字典消息信息")
    @ApiOperationSupport(order = 7)
    @PostMapping("update")
    public BaseResponse<SsModelInfo> updateByModelInfo(@RequestBody @Valid SsModelInfo ssModelInfo) {
        if (ObjectUtil.isEmpty(ssModelInfo.getId())) {
            throw new DataException("更新数据时, id 不能为空");
        }
        ssModelInfo.setUpdateUser(SecurityUtils.getLoginName());
        ssModelInfo.setUpdateDate(DateUtil.now());
        this.ssModelInfoService.updateById(ssModelInfo);
        return BaseResponse.success();
    }

    @OpsLog(value = "查询典消息所有列表", type = OpsLogTypeEnum.SELECT)
    @GetMapping("list/all")
    @ApiOperationSupport(order = 9)
    @ApiOperation(value = "查询典消息所有列表")
    @ApiImplicitParam(value = "是否绑定消息", name = "isBindMsg", paramType = "query")
    public BaseResponse<List<SsModelInfo>> pageDictAll(QueryRequest queryRequest, @RequestParam(defaultValue = "false") boolean isBindMsg) {
        Page<SsModelInfo> result = this.ssModelInfoService.pageModelInfoAll(queryRequest);
        if (isBindMsg) {
            List<MessageType> messageTypes = this.messageTypeService.selectAllMessageTypeList(new MessageType());
            List<String> bind = messageTypes.stream().map(MessageType::getTypeName).collect(Collectors.toList());
            return BaseResponse.success(result.getRecords().stream().filter(item -> !CollUtil.contains(bind, item.getModelCode())).collect(Collectors.toList()));
        }
        return CollUtil.isNotEmpty(result.getRecords()) ? BaseResponse.success(result.getRecords()) : BaseResponse.error(String.valueOf(CodeAndMessageEnum.DB_RESOURCE_NULL.getCode()), CodeAndMessageEnum.DB_RESOURCE_NULL.getMessage());
    }

}
